<template>
  <view class="chat-time" v-if="showTime">
    <text>{{ formatSimpleDateTime(item.createTime) }}</text>
  </view>
  <view class="chat-body" :class="leftFlag(item) ? 'chat-left' : 'chat-right'">
    <view class="chat-header">
      <i-image
        borderRadius="20rpx"
        :src="userInfo.face || '/static/default.png'"
      ></i-image>
      <view>
        <text v-if="!leftFlag(item)" style="color: #007aff">{{
          userInfo.nickName
        }}</text>
        <text v-if="leftFlag(item)">{{ username.name }}</text>
      </view>
    </view>
    <view class="box" :class="leftFlag(item) ? 'left' : 'right'" />
    <view class="msg">
      <rich-text :nodes="contentReplace(item.text)"></rich-text>
      <!-- <image-text v-if="item.type === 1" :message="item.text"></image-text>
      <video-text v-if="item.type === 2" :message="item.text"></video-text>
      <audio-text v-if="item.type === 3" :Audio="Audio" :item="item" :leftFlag="leftFlag(item)"></audio-text> -->
    </view>
    <view
      class="readType"
      v-if="!leftFlag(item) && !props.groupFlag"
      text-24rpx
    >
      <text v-if="item.isRead" style="color: #8f949f">已读</text>
      <text v-else>未读</text>
    </view>
  </view>
</template>

<script setup lang="ts">
import IImage from '@/pages/im/components/chat/components/i-image.vue'
import { faceUtils } from '@/pages/im/components/chat/components/util'
import videoText from '@/pages/im/components/chat/components/video-text.vue'
import audioText from '@/pages/im/components/chat/components/audio-text.vue'
import ImageText from '@/pages/im/components/chat/components/image-text.vue'
import storage from '@/utils/storage'

const props = defineProps({
  item: {
    // 页码
    type: Object,
    default: {}
  },
  //当前聊天用户
  username: {
    type: Object,
    default: () => {}
  },
  //是否组聊天
  groupFlag: {
    type: Boolean,
    default: false
  },
  showTime: {
    type: Boolean,
    default: false
  },
  Audio: {
    type: Object
  }
})

const userInfo = storage.getUserInfo()

//是否他人消息
const leftFlag = (item: any): boolean => {
  return item.fromUser !== userInfo.id
}

const formatSimpleDateTime = (time: string): string => {
  return time
}

const contentReplace = (content: string): string => {
  const fa = faceUtils.faces() as any
  content = content
    // 转义表情
    .replace(/\[([^\s\[\]]+?)]/g, function (face) {
      let alt = face.replace(/^/g, '')
      return (
        '<img class="emoji" style="width:28rpx;height:28rpx;"  src="' +
        fa[alt] +
        '"/>'
      )
    })
  return content
}
</script>

<style lang="scss" scoped>
$leftColor: #0a98d5;
$rightColor: #c3d0d5;

view.chat-time {
  text-align: center;
  margin-bottom: 5upx;

  text {
    background: #acb5c0;
    color: #fdfdfd;
    padding: 5upx 15upx 5upx 15upx;
    border-radius: 15upx;
    font-size: 22upx;
  }
}

view.chat-body {
  display: flex;
  justify-content: flex-start;
  align-items: center;

  .box {
    width: 0px;
    height: 0px;
    border-width: 10upx;
    border-style: solid;
    margin-top: 5upx;

    &.left {
      border-top-color: transparent;
      border-bottom-color: transparent;
      border-left-color: transparent;
    }

    &.right {
      border-top-color: transparent;
      border-right-color: transparent;
      border-bottom-color: transparent;
    }
  }

  .msg {
    min-height: 60upx;
    max-width: 480upx;
    font-size: 28upx;
    line-height: 30upx;
    padding: 10upx 20upx;
    display: flex;
    align-items: center;
    border-radius: 20upx;

    image {
      min-width: 250upx;
      max-width: 100%;
    }
  }

  view.readType {
    margin-right: 10upx;
    color: #b07474;
  }

  view.chat-header {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    flex-direction: column;
    color: #3f536e;

    & > view {
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      max-width: 130rpx;
      height: 30rpx;
      font-size: 22rpx;
      line-height: 30rpx;
    }
  }

  &.chat-left {
    color: #ffffff;
    margin-left: 15upx;

    & > .msg {
      background: $leftColor;
    }

    & > .box {
      border-right-color: $leftColor;
    }
  }

  &.chat-right {
    flex-direction: row-reverse;
    margin-right: 15upx;

    & > .msg {
      background: $rightColor;
      word-break: break-all;
    }

    & > .box {
      border-left-color: $rightColor;
    }
  }

  view.chat-content {
    background: #007aff;
    max-width: 80vw;
  }
}
</style>
